/****************************************************************************
 * Copyright (C) 2011 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 *     Description: MAXQ622 serial communications interface driver
 *             $Id: comm.c 1935 2011-08-21 13:45:06Z jbrodt $
 *
 ********************************************************************************
 */

#include <stdio.h>
#include "maxq_config.h"
#include "comm\maxq_comm.h"

/***** Definitions *****/
#define PIN2    0x04

#define NONBLOCKING


/****************************************************************************/
void comm_init(void)
{
  PO0 |= PIN2;   // Hold Tx0 line High (P0.2)
  PD0 |= PIN2;   // Port as output
  
  SMD0 = 0x02;         // Set baud rate select bit, no interrupt
  SCON0 = 0x50;        // Set mode 1 and receive enable for UART 0
  PR0 = COMM_PR_VALUE; // Set baud rate using phase register
}


/****************************************************************************/
void comm_deinit(void)
{
  SCON0 = 0;
  SMD0 = 0;

  comm_tx_disable();

  PD0 &= ~PIN2;   // Port as input
  PO0 |= PIN2;   // Weak pull-up enabled
}


/****************************************************************************/
int putchar(int byte)
{
  SCON0 &= ~2;    // clear TI flag
  SBUF0 = byte;   // write byte to data register
  while(!(SCON0 & 2));  // wait for TI flag
  SCON0 &= ~2;     // clear TI flag
  return byte;    // return sent byte
}

/****************************************************************************/
// This function implementation is needed to override putchar from DLIB
int __ungetchar(int c) { return 0; }

/****************************************************************************/
int getchar(void)
{
  int byte;

  while (!(SCON0 & 1)); // Wait for receive complete flag to be set
  SCON0 &= ~1;          // Clear receive complete flag
  byte = SBUF0;
#ifdef COMM_ECHO
  putchar(byte);
#endif

  return byte;
}


/****************************************************************************/
void comm_tx_enable(void) {
}


/****************************************************************************/
void comm_tx_disable(void) {
}

